package com.hmall;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;

import com.hmall.pojo.ItemDto;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

/**
 * @author 长夜漫漫，代码相伴
 * @date 2022/6/20
 */
@SpringBootTest
public class EsDemo {


    private RestHighLevelClient client;

    @Autowired
    private IItemService itemService;
    @Test
//新增一条数据
    void testAddDocument() throws IOException {
        // 1.查询数据库hotel数据
        Item item = itemService.getById(317578);
        // 2.转换为ItemDto
        ItemDto itemDto = new ItemDto(item);
        // 3.转JSON
        String json = JSON.toJSONString(itemDto);

        // 1.准备Request
        IndexRequest request = new IndexRequest("hm").id(itemDto.getId().toString());
        // 2.准备请求参数DSL，其实就是文档的JSON字符串
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

    @Test//查询指定id的数据
    void testGetDocumentById() throws IOException {
        // 1.准备Request      // GET /hotel/_doc/{id}
        GetRequest request = new GetRequest("hm", "317578");
        // 2.发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

        Item item = JSON.parseObject(json, Item.class);
        System.err.println("查询到的数据是 = " + item);
    }

    @Test
    void testBulkRequest() throws IOException {
        // 1.准备Request
        BulkRequest request = new BulkRequest();
        // 查询所有的酒店数据
//        List<Item> list = itemService.list();
        for (int i = 1; i <= 4; i++) {
            Page<Item> page = new Page<>(i,500);
            Page<Item> page1 = itemService.page(page);
            for (Item item : page1.getRecords()) {
                ItemDto itemDto = new ItemDto(item);
                // 3.转JSON
                String json = JSON.toJSONString(itemDto);

                // 2.3.添加请求
                request.add(new IndexRequest("hm").id(item.getId().toString()).source(json, XContentType.JSON));
            }
        }

//        // 1.准备Request
//        BulkRequest request = new BulkRequest();
//        // 2.准备参数
//        for (Item item : list) {
//            ItemDto itemDto = new ItemDto(item);
//            // 3.转JSON
//            String json = JSON.toJSONString(itemDto);
//
//            // 2.3.添加请求
//            request.add(new IndexRequest("hm").id(item.getId().toString()).source(json, XContentType.JSON));
//        }
        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);
    }

    @Test
    void HelloWorld(){
        System.out.println("乌鸦坐飞机");
    }

    @BeforeEach
    void setUp() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.74.100:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}
